package com.lgq.cfw.service.impl;

import com.lgq.cfw.bean.TableCert;
import com.lgq.cfw.bean.example.TableCertExample;
import com.lgq.cfw.bean.example.TableCertExample.Criteria;
import com.lgq.cfw.dao.TableCertMapper;
import com.lgq.cfw.service.CertService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Lin
 * @date 2019-02-11 19:48
 **/
@Service
public class CertServiceImpl implements CertService {

  @Autowired
  TableCertMapper tableCertMapper;

  /**
   * 获取资质信息
   * @return 资质的list
   */
  @Override
  public List<TableCert> getAllCert() {
    return tableCertMapper.selectByExample(null);
  }

  /**
   * 新增
   * @param certName 资质名称
   * @return 布尔
   */
  @Override
  public boolean addCert(String certName) {
    TableCert tableCert=new TableCert();
    tableCert.setName(certName);
    int row=0;
    try {
      row=tableCertMapper.insertSelective(tableCert);
    }catch (Exception e){
      e.printStackTrace();
      return false;
    }
    return row==1;
  }

  /**
   * 更新资质
   * @param tableCert 前台传入的资质信息
   * @return 是否成功
   */
  @Override
  public boolean updateCert(TableCert tableCert) {
    try {
      tableCertMapper.updateByPrimaryKey(tableCert);
    }catch (Exception e){
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * 批量单个删除
   * @param certDeleteIdString 资质id的字符串
   * @return 是否成功
   */
  @Override
  public boolean deleteCert(String certDeleteIdString) {
    if (!certDeleteIdString.contains("-")){
      try{
        tableCertMapper.deleteByPrimaryKey(Integer.parseInt(certDeleteIdString));
      }catch (Exception e){
        e.printStackTrace();
        return false;
      }
      return true;
    }else {
      List<Integer> deleteIdList=new ArrayList<>();
      String[] deleteIdStringArray = certDeleteIdString.split("-");
      for (String deleteIdString : deleteIdStringArray) {
        deleteIdList.add(Integer.parseInt(deleteIdString));
      }
      TableCertExample tableCertExample=new TableCertExample();
      Criteria criteria=tableCertExample.createCriteria();
      criteria.andIdIn(deleteIdList);
      try{
        tableCertMapper.deleteByExample(tableCertExample);
      }catch (Exception e){
        e.printStackTrace();
        return false;
      }
      return true;
    }
  }
}
